package com.boyaa.shells.manager.dragManager 
{
	import com.boyaa.shells.control.GlobalVar;
	import com.boyaa.shells.control.LayerManager;
	import com.boyaa.shells.data.model.GoodsData;
	import com.boyaa.shells.data.model.ShopData;
	import com.boyaa.shells.net.load.MyLoader;
	import com.boyaa.shells.tool.ContainerClear;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	/**
	 * ...
	 * @author fly2011...
	 */
	public class DragManager 
	{
		static private var _dragImage:Sprite = new Sprite();
		static private var _callBack:Function;
		static private var _callBack1:Function;
		static private var _targets:Array;
		static private var _loader:MyLoader;
		static private var _data:ShopData;
		
		/**
		 *  传人一个要拖动的物品，如果鼠标MOUSE_UP碰到acceptTargets指定的对象callBack（），其他callBack1!=null && callBack1()；
		 * @param	data
		 * @param	callBack
		 * @param	acceptTargets
		 * @param	callBack1  回调带上鼠标的舞台位置
		 */
		static public function doDrag(data:ShopData, callBack:Function, acceptTargets:Array, callBack1:Function = null) :void
		{
			_data = data;
			_loader = new MyLoader();
			var level:int = 1;
			if (data is GoodsData)
				level = (data as GoodsData).iconID();
			_loader.getRes(data.typeID, data.frameID, data.color, setDragImage, null, "Face", level);
			_targets = acceptTargets;
			_callBack = callBack;
			_callBack1 = callBack1;
			GlobalVar.instance.getMain().stage.addEventListener(MouseEvent.MOUSE_UP, dropIt);
		}
		
		// 
		static private function dropIt(e:MouseEvent):void 
		{
			GlobalVar.instance.getMain().stage.removeEventListener(MouseEvent.MOUSE_UP, dropIt);
			if (_dragImage.parent)
			{
				LayerManager.instance.getLayer(LayerManager.SCORE_LAYER).removeChild(_dragImage);
				_dragImage.removeChildAt(0);
			}
			else
				_loader.getRes(0, 0,0, null);
			var pt:Point = new Point(e.stageX, e.stageY);
			for each(var el:* in _targets)
			{
				if (el.content.hitTestPoint(pt.x, pt.y))
				{
					if (_callBack != null)
						_callBack(el, _data);
					remove();
					return;
				}
			}
			if (_callBack1 != null)
				_callBack1(pt, _data);
			remove();
		}
		
		static private function remove():void
		{
			_callBack = null;
			_callBack1 = null;
			_loader = null;
			_data = null;
			_targets = null;
			ContainerClear.removeAllChild(_dragImage);
		}
		
		// 拖动
		static private function setDragImage(dragImage:Sprite):void
		{
			_dragImage.addChild(dragImage);
			if (dragImage.width >= dragImage.height)
			{
				dragImage.width = 50;
				dragImage.scaleY = dragImage.scaleX;
			}
			else//否则，就根据高度来缩放
			{
				dragImage.height = 50;
				dragImage.scaleX = dragImage.scaleY;
			}
			var rect:Rectangle = dragImage.getBounds(dragImage.parent);
			dragImage.x -=  (rect.x + rect.width / 2);
			dragImage.y -=  (rect.y + rect.height / 2);
			LayerManager.instance.addToLayer(LayerManager.SCORE_LAYER, _dragImage);
			
			_dragImage.startDrag(true);
			
		}
	}

}